package com.cuit.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.Book.BookTypeMapper;
import com.cuit.mapper.Book.BookUserMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.BookType;
import com.cuit.pojo.BookUser;
import com.cuit.result.R;
import com.cuit.service.Book.BookUserService;
import com.cuit.service.Book.GuessLikeService;
import com.cuit.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@Api(tags = "书本类型")
@RestController
@RequestMapping("/book")
public class BookTypeController {
    @Resource
    private BookTypeMapper bookTypeMapper;
    @Resource
    private FileService fileService;
    @Resource
    private BookUserMapper bookUserMapper;
    @Resource
    private GuessLikeService guessLikeService;



    @PostMapping("/bookType")
    @ApiOperation("获取图书所有分类信息")
    public R findBookType(){
        List<BookType> bookList = bookTypeMapper.selectList(null);
        return  R.ok().data("bookTypeList",bookList);
    }
    @PostMapping("/bookByType")
    @ApiOperation("获取对应图书的信息")
    public  R findBookByType(@RequestBody String type){
        List<BookType> TypeList = bookTypeMapper.selectList(null);
        for(int i=0;i<TypeList.size();i++){
            if(type.contains(TypeList.get(i).getTypeName()) ){
                type=TypeList.get(i).getTypeName();
                break;
            }
        }
        List<BookUser> bookUserList=null;
      /*  if (type=="猜你喜欢"){
            bookUserList=bookUserMapper.selectList(null);
            return  R.ok().data("bookTypeList",bookUserList);
        }*/
        QueryWrapper<BookUser>  queryWrapper= new QueryWrapper();
        queryWrapper.like("bookTypeName",type);
        bookUserList = bookUserMapper.selectList(queryWrapper);
        bookUserList.forEach(bookUser -> {
            bookUser.setUserImg(fileService.allPath(bookUser.getUserImg()));
            bookUser.setBookImg(fileService.allPath(bookUser.getBookImg()));
        });
        return  R.ok().data("bookTypeList",bookUserList);
    }
    @PostMapping("/bookByIdType")
    @ApiOperation("获取对应图书的信息")
    public  R findBookIdType(){
        Integer bookId = guessLikeService.getBookId();
        BookUser bookUser = bookUserMapper.selectById(bookId);
        String type=bookUser.getBookType();
        List<BookUser> bookUserList=null;
      /*  if (type=="猜你喜欢"){
            bookUserList=bookUserMapper.selectList(null);
            return  R.ok().data("bookTypeList",bookUserList);
        }*/
        QueryWrapper<BookUser>  queryWrapper= new QueryWrapper();
        queryWrapper.like("bookTypeName",type);
        bookUserList = bookUserMapper.selectList(queryWrapper);
        bookUserList.forEach(bookU -> {
            bookU.setUserImg(fileService.allPath(bookUser.getUserImg()));
            bookU.setBookImg(fileService.allPath(bookUser.getBookImg()));
        });
        return  R.ok().data("bookTypeList",bookUserList);
    }
    @GetMapping("/bookLikeId/{userNameId}")
    @ApiOperation("图书id")
    public  R  setLikeBookId(@PathVariable String userNameId){
        String[] userNameIdArr = userNameId.split(",");
        Integer bookId = Integer.valueOf(userNameIdArr[0]);
        List<BookUser> likeBookList=null;
        //长度等于一一位置没有用户登陆，拆查询所有商品返回前端
        if(userNameIdArr==null||userNameIdArr.length==1){
            likeBookList = bookUserMapper.selectList(null);
        }else {
            //不为空接存入reduis
            guessLikeService.increase(bookId,userNameIdArr[1]);
            //得到喜欢的书本信息
            likeBookList = guessLikeService.getLikeBook(userNameIdArr[1]);

        }
        return R.ok().data("bookUser",likeBookList);
    }

    //获取喜欢的图书
    @GetMapping("/getLikeBook/{username}")
    @ApiOperation("获取详细信息")
    public  R getLikeBook(@PathVariable String username){
        System.out.println(username+"===============");
        List<BookUser> likeBookList=null;
        if(username.equals("null")){
            System.out.println("进来了");
            likeBookList = bookUserMapper.selectList(null);
            System.out.println(likeBookList);
            return R.ok().data("bookLikeList",likeBookList);
        }
        likeBookList = guessLikeService.getLikeBook(username);

        return R.ok().data("bookLikeList",likeBookList);
    }
    //没有用户查找找商品
    @PostMapping("/getBookNullUser")
    @ApiOperation("查找说有书籍")
    public  R AllBookUser(){
        System.out.println("进来了");
        List<BookUser> bookUserList = bookUserMapper.selectList(null);
        bookUserList.forEach(bookUser -> {
            bookUser.setUserImg(fileService.allPath(bookUser.getUserImg()));
            bookUser.setBookImg(fileService.allPath(bookUser.getBookImg()));
        });
        return R.ok().data("bookLikeList",bookUserList);
    }
    //bookid存到redisli
    @GetMapping("/getBookId/{id}")
    @ApiOperation("存id")
    public R setBookId(@PathVariable Integer id){
        System.out.println("====================================="+id);
        guessLikeService.setBookid(id);
        return R.ok().data("dd","dd");
    }
    //获取bookid并查询相关信息
    @PostMapping("/getBookUser")
    @ApiOperation("通过id过去图书信息")
    public R getBookInfo(){
        Integer bookId = guessLikeService.getBookId();
        BookUser bookUser = bookUserMapper.selectById(bookId);
        bookUser.setUserImg(fileService.allPath(bookUser.getUserImg()));
        bookUser.setBookImg(fileService.allPath(bookUser.getBookImg()));
        return  R.ok().data("bookUser",bookUser);
    }

    @GetMapping("/book/detail/{id}")
    @ApiOperation("图书详情页信息")
    public  R  findBookDetail(@PathVariable Integer id){
        BookUser bookUser = bookUserMapper.selectById(id);
        System.out.println("===========================================");
        System.out.println(bookUser.toString());
        return R.ok().data("bookUser",bookUser);
    }


    private Jedis jedis = new Jedis("47.109.38.52");
    @GetMapping("/subscribe")
    public void subscribe(String username,String name){
        //在关注列表中添加一条
        jedis.lpush(username,name);
        //在对方的粉丝列表中添加
        jedis.lpush(name+"fans",username);
//        jedis.close();
//        System.out.println("guanzhude: "+username+" beiguanzhude: "+name);
    }

    @GetMapping("/unSubscribe")
    public void unSubscribe(String username, String name){
        jedis.lrem(username,1,name);
//        jedis.close();
        jedis.lrem(name+"fans",1,username);
    }

    //进页面时判断是否已关注
    @GetMapping("/ifSubscribe")
    public R ifSubscribe(String username, String name){
        jedis.auth("123456");
        String flag = "false";
        if (jedis.exists(username)){
            Long length = jedis.llen(username);

            for (int i = 0;i < length;i++){
                if (name.equals(jedis.lindex(username,i))){
                    flag = "true";
                }
            }
        }
//        jedis.close();
        return R.ok().data("ifSub2",flag);
    }
}
